define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user_relation/index' + location.search,
                    // add_url: 'user/user_relation/add',
                    // edit_url: 'user/user_relation/edit',
                    // del_url: 'user/user_relation/del',
                    multi_url: 'user/user_relation/multi',
                    import_url: 'user/user_relation/import',
                    table: 'user_relation',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);

                $("input[name='user_id']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("orderBy", "id desc");

                $("input[name='p_user_id']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("orderBy", "id desc");

                $("input[name='user_id_1']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("params", function (obj) {
                        //obj为SelectPage对象
                        return {custom: {group_id: 1}};
                    })
                    .data("orderBy", "id desc");

                $("input[name='user_id_2']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("params", function (obj) {
                        //obj为SelectPage对象
                        return {custom: {group_id: 2}};
                    })
                    .data("orderBy", "id desc");

                $("input[name='user_id_3']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("params", function (obj) {
                        //obj为SelectPage对象
                        return {custom: {group_id: 3}};
                    })
                    .data("orderBy", "id desc");

                $("input[name='user_id_4']", form)
                    .addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "id")
                    .data("field", "nickname")
                    .data("params", function (obj) {
                        //obj为SelectPage对象
                        return {custom: {group_id: 4}};
                    })
                    .data("orderBy", "id desc");

                $("input[name='group_id']", form)
                    .addClass("selectpage")
                    .data("source", "user/group/selectpage")
                    .data("primaryKey", "id")
                    .data("field", "name")
                    .data("orderBy", "id desc");

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'user_id',
                sortName: 'user_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                commonSearch: true,
                searchFormVisible: true,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'user_id', title: __('当前用户ID'), operate: 'LIKE %...%', sortable: true},
                        {field: 'user.nickname', title: __('User_id'), operate: false},
                        {field: 'group_id', title: __('当前用户身份ID'), visible:false, operate: 'LIKE %...%'},
                        {field: 'group.name', title: __('Group_id'), operate: false},
                        {field: 'p_user_id', title: __('P_user_id'), visible:false, operate: 'LIKE %...%'},
                        {field: 'puser.nickname', title: __('P_user_id'), operate: false},
                        {field: 'num', title: __('Num'), operate: 'BETWEEN', sortable: true},
                        // {field: 'user_id_1', title: __('User_id_1'), visible:false, operate: 'LIKE %...%'},
                        // {field: 'user1.nickname', title: __('User_id_1'), operate: false},
                        // {field: 'user_id_2', title: __('User_id_2'), visible:false, operate: 'LIKE %...%'},
                        // {field: 'user2.nickname', title: __('User_id_2'), operate: false},
                        // {field: 'user_id_3', title: __('User_id_3'), visible:false, operate: 'LIKE %...%'},
                        // {field: 'user3.nickname', title: __('User_id_3'), operate: false},
                        // {field: 'user_id_4', title: __('User_id_4'), visible:false, operate: 'LIKE %...%'},
                        // {field: 'user4.nickname', title: __('User_id_4'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
